package com.jlf.core.engine.entity;

import com.jlf.core.engine.domain.SchemaFieldDO;
import com.jlf.core.engine.domain.SchemaIndexDO;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据表的元数据
 *
 * @author wujr
 * 2023/7/14
 * @version 1.0
 * 【变更历史】
 * [wujr 2023/7/14 1.0 新增]
 */
@Getter
@Setter
public class MetaTable {
    /**
     * 数据表名称
     */
    private String name;
    private String catalog;
    private String schema;
    private String type;
    private String remark;
    private final Map<String, MetaColumn> columns = new HashMap<>();
    private final Map<String, MetaIndex> indexes = new HashMap<>();

    public void addColumn(MetaColumn column){
        this.columns.put(column.getName(), column);
    }
    public void addIndex(MetaIndex index){
        this.indexes.put(index.getName(), index);
    }

    public boolean difference(List<SchemaFieldDO> schemaFields,
                              List<SchemaIndexDO> schemaIndexes,
                              List<SchemaFieldDO> newFields, List<SchemaFieldDO> modFields, List<SchemaFieldDO> delFields,
                              List<SchemaIndexDO> newIndexes, List<SchemaIndexDO> modIndexes, List<SchemaIndexDO> delIndexes){
        List<String> fieldCodes = new ArrayList<>();
        boolean altered = false;
        for (SchemaFieldDO schemaField : schemaFields){
            fieldCodes.add(schemaField.getJdbcFieldCode());
            MetaColumn column = columns.get(schemaField.getJdbcFieldCode());
            if (column == null){
                // 这是新增的字段
                newFields.add(schemaField);
                altered = true;
            }else{
                if (column.isAltered(schemaField)){
                    modFields.add(schemaField);
                    altered = true;
                }
            }
        }
        List<SchemaFieldDO> removedFields = schemaFields.stream().filter(o -> !fieldCodes.contains(o.getJdbcFieldCode())).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(removedFields)){
            delFields.addAll(removedFields);
            altered = true;
        }
        if (!CollectionUtils.isEmpty(schemaIndexes)){
            List<String> indexNames = new ArrayList<>();
            for (SchemaIndexDO schemaIndex : schemaIndexes){
                indexNames.add(schemaIndex.getName());
                MetaIndex metaIndex = indexes.get(schemaIndex.getName());
                if (metaIndex == null){
                    // 这是新增的索引
                    newIndexes.add(schemaIndex);
                    altered = true;
                }else{
                    if (metaIndex.isAltered(schemaIndex)){
                        modIndexes.add(schemaIndex);
                        altered = true;
                    }
                }
            }
            List<SchemaIndexDO> removedIndexes = schemaIndexes.stream().filter(o -> !indexNames.contains(o.getName())).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(removedIndexes)){
                delIndexes.addAll(removedIndexes);
                altered = true;
            }
        }
        return altered;
    }

    public void removeColumn(String columnName){
        this.columns.remove(columnName);
    }
}
